﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortWithDelegates
{
    /// <summary>
    /// Person Class for delegates
    /// </summary>
    public class PersonDelegates
    {
        #region Constructors

        /// <summary>
        /// Empty constructor
        /// </summary>
        public PersonDelegates()
            : this(0, string.Empty, string.Empty, 0)
        {
        }

        /// <summary>
        /// Full constructor for person
        /// </summary>
        /// <param name="code">Person's code</param>
        /// <param name="firstname">Person's firstname</param>
        /// <param name="lastname">Person's lastname</param>
        /// <param name="bdate">Person's birthday</param>
        public PersonDelegates(int code, string firstName, string lastName, int age)
        {
            this.Code = code;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Comparing persons by Code
        /// </summary>
        /// <param name="person1">First Person for comparing</param>
        /// <param name="person2">Second Person for comparing</param>
        /// <returns></returns>
        private static bool CompareCode(PersonDelegates person1, PersonDelegates person2)
        {
            bool result = true;
            if (person1.Code > person2.Code)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by Age
        /// </summary>
        /// <param name="person1">First Person for comparing</param>
        /// <param name="person2">Second Person for comparing</param>
        /// <returns></returns>
        private static bool CompareAge(PersonDelegates person1, PersonDelegates person2)
        {
            bool result = true;
            if (person1.Age > person2.Age)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by FirstName
        /// </summary>
        /// <param name="person1">First Person for comparing</param>
        /// <param name="person2">Second Person for comparing</param>
        /// <returns></returns>
        private static bool CompareFirstName(PersonDelegates person1, PersonDelegates person2)
        {
            bool result = true;
            for (int i = 0; i < (person1.FirstName.Length > person2.FirstName.Length ? person2.FirstName.Length : person1.FirstName.Length); i++)
            {
                if (person1.FirstName.ToCharArray()[i] > person2.FirstName.ToCharArray()[i]) return false;
                if (person1.FirstName.ToCharArray()[i] < person2.FirstName.ToCharArray()[i]) return true;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by LastName
        /// </summary>
        /// <param name="person1">First Person for comparing</param>
        /// <param name="person2">Second Person for comparing</param>
        /// <returns></returns>
        private static bool CompareLastName(PersonDelegates person1, PersonDelegates person2)
        {
            bool result = true;
            for (int i = 0; i < (person1.LastName.Length > person2.LastName.Length ? person2.LastName.Length : person1.LastName.Length); i++)
            {
                if (person1.LastName.ToCharArray()[i] > person2.LastName.ToCharArray()[i]) return false;
                if (person1.LastName.ToCharArray()[i] < person2.LastName.ToCharArray()[i]) return true;
            }
            return result;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Person Code
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// Person FirstName
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// Person LastName
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// Person Age
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// Mapping out comparing persons by Code
        /// </summary>
        public static PersonsDelegates.CompareDelegates SortByCode
        {
            get { return (new PersonsDelegates.CompareDelegates(CompareCode)); }
        }

        /// <summary>
        /// Mapping out comparing persons by Age
        /// </summary>
        public static PersonsDelegates.CompareDelegates SortByAge
        {
            get { return (new PersonsDelegates.CompareDelegates(CompareAge)); }
        }

        /// <summary>
        /// Mapping out comparing persons by FirstName
        /// </summary>
        public static PersonsDelegates.CompareDelegates SortByFirstName
        {
            get { return (new PersonsDelegates.CompareDelegates(CompareFirstName)); }
        }

        /// <summary>
        /// Mapping out comparing persons by LastName
        /// </summary>
        public static PersonsDelegates.CompareDelegates SortByLastName
        {
            get { return (new PersonsDelegates.CompareDelegates(CompareLastName)); }
        }

        #endregion Properties

    }
}
